#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          kopano-presence
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Should-Start:      kopano-server
# Should-Stop:       kopano-server
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# X-UnitedLinux-Default-Enabled:   yes
# Short-Description: Kopano Core Presence Daemon
### END INIT INFO

. /etc/rc.status
rc_reset

PRESENCECONFIG=/etc/kopano/presence.cfg
PRESENCEPROGRAM=/usr/sbin/kopano-presence

# Sanity checks.
#[ -f $PRESENCECONFIG ] || exit 0
[ -x $PRESENCEPROGRAM ] || exit 0

PRESENCECONFIG_OPT=""
[ ! -z $PRESENCECONFIG -a -f $PRESENCECONFIG ] && PRESENCECONFIG_OPT="-c $PRESENCECONFIG"

[ -f /etc/sysconfig/kopano ] && . /etc/sysconfig/kopano
if [ -z "$KOPANO_LOCALE" ]; then
	KOPANO_LOCALE="C"
fi

presence=`basename $PRESENCEPROGRAM`

case "$1" in
    start)
		echo -n "Starting presence "
		install -dm0775 -o kopano -g kopano /var/run/kopano
		export LC_ALL=$KOPANO_LOCALE
		export LANG=$KOPANO_LOCALE
		$PRESENCEPROGRAM $PRESENCECONFIG_OPT
		rc_status -v
		unset LC_ALL LANG
		;;

    stop)
		echo -n "Stopping presence "
		killproc -t15 $presence
		rc_status -v
		;;

    restart)
		$0 stop
		$0 start
		;;

	reload)
		echo -n "Restarting presence: "
		killproc $presence -SIGHUP
		rc_status -v
		;;

    status)
		echo -n "Checking for presence "
		checkproc $presence
		rc_status -v
		;;

    *)
		;;
esac

rc_exit
